package com.tbeasy.financial.provider;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.tbeasy.financial.provider.DatabaseUtil.TableCategory;
import com.tbeasy.financial.provider.DatabaseUtil.TableTransaction;

public class DatabaseHelper extends SQLiteOpenHelper {

	public static final int DATABASE_VERSION = 1;
	private Context mContext;

	public static final String CT_CATEGORY = "CREATE TABLE " + TableCategory.TAB_NAME + " (" + TableCategory.CATEGORY_ID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT," + TableCategory.NAME + " TEXT," + TableCategory.PARENT_CATEGORY_ID + " INTEGER,"
			+ TableCategory.LAST_UPDATE_TIME + " INTEGER" + TableCategory.ICON_NAME + " TEXT," + TableCategory.ORDERED + " INTEGER" + ");";

	public static final String CT_TRANSACTION = "CREATE TABLE " + TableTransaction.TAB_NAME + " (" + TableTransaction.TRANSACTION_ID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT," + TableTransaction.CREATE_TIME + " LONG," + TableTransaction.MODIFIED_TIME + " LONG,"
			+ TableTransaction.TRADE_TIME + " LONG," + TableTransaction.MEMO + " TEXT," + TableTransaction.TYPE + " INTEGER,"
			+ TableTransaction.CATEGORY_ID + " INTEGER," + TableTransaction.BUYER_MONEY + " decimal(12,2),"
			+ TableTransaction.LAST_UPDATE_TIME + "LONG" + ");";

	public DatabaseHelper(Context context) {
		super(context, DatabaseUtil.DB_NAME, null, DATABASE_VERSION);
		mContext = context;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(CT_CATEGORY);
		db.execSQL(CT_TRANSACTION);
	}

	public void dropView() {
		SQLiteDatabase db = getWritableDatabase();
		db.execSQL("drop view  if exists view_budget");
	}

	public void addCategoryData() {

		ArrayList<Map<String, String>> initDatas = parseXml("category.xml");
		if (initDatas == null || initDatas.isEmpty()) {
			return;
		}
		ContentValues values = null;
		for (int i = 0; i < initDatas.size(); i++) {
			Map<String, String> mapValue = initDatas.get(i);
			values = new ContentValues();
			values.put(TableCategory.NAME, mapValue.get("name"));
			values.put(TableCategory.PARENT_CATEGORY_ID, mapValue.get("pid"));
			values.put(TableCategory.CATEGORY_ID, mapValue.get("cid"));
			values.put(TableCategory.LAST_UPDATE_TIME, System.currentTimeMillis());
			mContext.getContentResolver().insert(TableCategory.CONTENT_URI, values);
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS" + TableCategory.TAB_NAME);
		db.execSQL("DROP TABLE IF EXISTS" + TableTransaction.TAB_NAME);
		onCreate(db);
	}

	public ArrayList<Map<String, String>> parseXml(String filename) {
		ArrayList<Map<String, String>> items = new ArrayList<Map<String, String>>();

		Map<String, String> map = null;

		try {
			XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
			factory.setNamespaceAware(true);
			XmlPullParser parser = factory.newPullParser();
			parser.setInput(mContext.getAssets().open(filename), "utf-8");
			int eventType = parser.getEventType();
			while (true) {

				if (eventType == XmlPullParser.START_DOCUMENT) {

				} else if (eventType == XmlPullParser.START_TAG) {
					switch (parser.getDepth()) {
					case 1:
						break;
					case 2:
						map = new HashMap<String, String>();
						String name = parser.getAttributeValue(null, "name");
						map.put("name", name);
						String pid = parser.getAttributeValue(null, "pid");
						map.put("pid", pid);
						String cid = parser.getAttributeValue(null, "cid");
						map.put("cid", cid);
						items.add(map);
						break;
					}

				} else if (eventType == XmlPullParser.END_TAG) {
				} else if (eventType == XmlPullParser.TEXT) {
				} else if (eventType == XmlPullParser.END_DOCUMENT) {
					break;
				}

				eventType = parser.next();

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return items;
	}
}
